package com.wswy.wyjk.ui.practice.exam

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.raft.framework.base.RFBaseFragment
import com.wswy.wyjk.AppContext
import com.wswy.wyjk.R
import com.wswy.wyjk.core.App
import com.wswy.wyjk.model.PracticeType
import com.wswy.wyjk.model.vo.ExamResult
import com.wswy.wyjk.ui.common.Router
import com.wswy.wyjk.ui.widget.CircleProgressDrawable
import com.wswy.wyjk.utils.Tools
import kotlinx.android.synthetic.main.fragment_exam_result.*

/**
 * 考试结果
 */
class ExamResultFragment : RFBaseFragment() {

    companion object {
        fun newInstance():RFBaseFragment{
            return ExamResultFragment()
        }
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_exam_result
    }

    override fun getPageTitle(): String? {
        return "模拟考试"
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)


        ExamCenter.calResult()?.let {

            showResult(it)
        }?:kotlin.run {

            finish()
            return
        }


    }

    private fun showResult(examResult: ExamResult){

        App.daoSession?.saveExamRecord(examResult)

        if (examResult.isApproved){
            root_layout.setBackgroundResource(R.drawable.exam_result_qualified_bg)
            tv_exam_status.text = "合格"
        }else{
            root_layout.setBackgroundResource(R.drawable.exam_result_failed_bg)
            tv_exam_status.text = "不合格"
        }

        tv_exam_score.text = examResult.score.toString()

        tv_exam_time.text = getString(R.string.exam_used_time,Tools.formatSecondTime(examResult.time))

        rl_exam_info_layout.background = CircleProgressDrawable(examResult.score)

        tv_ct_info.text = SpannableStringBuilder().apply {

            append("答错 ")
            append(SpannableString(examResult.wrongCount.toString()).apply {
                setSpan(ForegroundColorSpan(ContextCompat.getColor(AppContext.getContext(),R.color.textColorWrong)),0,length,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
            })
            append(" 题")
            if (examResult.undoneCount > 0){

                append("，未做 ")

                append(SpannableString(examResult.undoneCount.toString()).apply {
                    setSpan(ForegroundColorSpan(ContextCompat.getColor(AppContext.getContext(),R.color.textColorPrimary)),0,length,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
                })
                append(" 题")
            }

        }

        //查看错题
        layout_see_wrong_questions.setOnClickListener {
            if (examResult.wrongCount > 0){
                //
                Router.start(it.context, Router.PRACTICE,practiceType = examResult.practiceType.copy().apply {
                    type = PracticeType.TYPE_EXAM_WRONG_LIST

                })
                finish()
            }

        }
        //重新考试
        layout_re_exam.setOnClickListener {

            Router.start(it.context, Router.PRACTICE,practiceType = examResult.practiceType.copy().apply {

                type = PracticeType.TYPE_EXAM

            })

            finish()
        }

    }


}